package view;

import java.util.Enumeration;
import java.util.Vector;

import javax.microedition.io.file.FileSystemRegistry;

import lib.Bill;
import lib.HorizontalIconName;
import lib.PageTemplate;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.XYEdges;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;

public class BillListing extends PageTemplate
{
	ObjectChoiceField category;
	public BillListing()
	{
		
		super(true);
		
		HorizontalFieldManager categoryManager = new HorizontalFieldManager(USE_ALL_WIDTH);
		category = new ObjectChoiceField("Bill Category: ", categories());
		categoryManager.add(category);
		categoryManager.setPadding(new XYEdges(5, 5, 5, 5));
		add(categoryManager);
		
		Vector bills = bills();
		VerticalFieldManager billListings = getBillListings(bills);
		
		add(billListings);
		
		Bitmap addIcon = Bitmap.getBitmapResource("icons/addBillIcon.png");
		add(new HorizontalIconName(addIcon, "Add New Bill"));
				
				
		
		
	}
	
	private VerticalFieldManager getBillListings(Vector bills)
	{
		VerticalFieldManager listings = new VerticalFieldManager(USE_ALL_WIDTH);
		for(int i=0; i < bills.size(); i++)
		{
			Bill bill = (Bill)bills.elementAt(i);
			Background background = null;
			if(i % 2 == 0)
			{
				background = BackgroundFactory.createSolidBackground( BillCrumb.COLOR_ONE );
			}else
			{
				background = BackgroundFactory.createSolidBackground( BillCrumb.COLOR_TWO );
			}
			BillCrumb crumb = new BillCrumb(bill, background);
			listings.add(crumb);
		}
		return listings;
	}
	
	private String[] categories()
	{
		String[] billCats = Instances._db.getCategories();
		String[] cats = new String[billCats.length +1];
		
		cats[0] = "all";
		for(int i=0; i < billCats.length; i++)
		{
			cats[i+1] = billCats[i];
		}
		
		return cats;
	}
	
	private Vector bills()
	{
		return Instances._db.getBills();
	}

}
